package io.itit.ecp.admin.provide.action.controller.miniprogram;

import com.baomidou.mybatisplus.core.metadata.IPage;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import icu.mhb.mybatisplus.plugln.extend.Joins;
import io.itit.ecp.admin.provide.action.req.CarbonEmissionFactorQueryReq;
import io.itit.ecp.admin.provide.action.resp.CarbonEmissionFactorResp;
import io.itit.ecp.admin.server.entity.CarbonEmissionFactorEntity;
import io.itit.ecp.admin.server.entity.EnergyConsumptionTypeEntity;
import io.itit.grass.common.provide.action.resp.ResultResp;
import io.itit.grass.common.provide.action.validation.IQueryPage;
import io.itit.grass.web.wall.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：碳排放因子控制类
 * @Date：Create by 2023-11-23 16:28:52
 */
@Api(tags = "碳排放因子")
@RestController
@RequestMapping("/mini/carbonEmissionFactor")
public class MiniCarbonEmissionFactorController {

    @ApiOperation("分页查询")
    @PostMapping("queryPage")
    public ResultResp<IPage<CarbonEmissionFactorResp>> queryPage(@RequestBody @Validated(IQueryPage.class) CarbonEmissionFactorQueryReq req) {
        return ResultResp.success(buildQW(req).joinPage(PageUtils.getPage(req, CarbonEmissionFactorResp.class), CarbonEmissionFactorResp.class));
    }

    /**
     * 组装查询条件
     */
    private static JoinLambdaWrapper<CarbonEmissionFactorEntity> buildQW(CarbonEmissionFactorQueryReq req) {
        LocalDateTime start = null;
        LocalDateTime end = null;
        if (StringUtils.isNotEmpty(req.getSolidifyDateStart())) {
            start = LocalDateTime.parse(req.getSolidifyDateStart(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }
        if (StringUtils.isNotEmpty(req.getSolidifyDateEnd())) {
            end = LocalDateTime.parse(req.getSolidifyDateEnd(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }
        return Joins.of(CarbonEmissionFactorEntity.class)
                .selectAll()
                .leftJoin(EnergyConsumptionTypeEntity.class, EnergyConsumptionTypeEntity::getId, CarbonEmissionFactorEntity::getEnergyTypeId)
                .selectAs(EnergyConsumptionTypeEntity::getEnergyName, CarbonEmissionFactorResp::getEnergyTypeName)
                .end()
                .ge(Objects.nonNull(start), CarbonEmissionFactorEntity::getSolidifyDate, start)
                .le(Objects.nonNull(end), CarbonEmissionFactorEntity::getSolidifyDate, end)
                .eq(StringUtils.isNotEmpty(req.getEnergyTypeId()), CarbonEmissionFactorEntity::getEnergyTypeId, req.getEnergyTypeId())
                .eq(req.getFactor() != null, CarbonEmissionFactorEntity::getFactor, req.getFactor())
                .eq(StringUtils.isNotEmpty(req.getRemark()), CarbonEmissionFactorEntity::getRemark, req.getRemark())
                .eq(StringUtils.isNotEmpty(req.getDeptId()), CarbonEmissionFactorEntity::getDeptId, req.getDeptId())
                .in(CollectionUtils.isNotEmpty(req.getIds()), CarbonEmissionFactorEntity::getId, req.getIds())
                .last(StringUtils.isNotEmpty(req.getSortColumn()), PageUtils.lastOrderBy(req))
                .orderByDesc(StringUtils.isEmpty(req.getSortColumn()), CarbonEmissionFactorEntity::getSolidifyDate);
    }

}
